/*
 *   Copyright 2023 <a href="mailto:asialjim@hotmail.com">Asial Jim</a>
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.asialjim.encommunicate.rsa.filter;

import com.asialjim.encommunicate.base.rsa.util.RSAServerUtil;
import jakarta.servlet.ReadListener;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Objects;

/**
 * 基于 RSA 算法加密的 Http 请求包装器
 *
 * @author Copyright © <a href="mailto:asialjim@hotmail.com">Asial Jim</a>   Co., LTD
 * @version 1.0
 * @since 2023/2/27, &nbsp;&nbsp; <em>version:1.0</em>, &nbsp;&nbsp; <em>java version:17</em>
 */
@Slf4j
public class SimpleRSAHttpServletRequestWrapper extends HttpServletRequestWrapper {
    private final InputStream decryptInputStream;
    private final int contentLength;
    private int index;

    /**
     * Constructs a request object wrapping the given request.
     *
     * @param request the {@link HttpServletRequest} to be wrapped.
     * @throws IllegalArgumentException if the request is null
     */
    public SimpleRSAHttpServletRequestWrapper(HttpServletRequest request, RSAServerUtil rsaServerUtil) throws IOException {
        super(request);
        this.decryptInputStream = rsaServerUtil.decrypt(request.getInputStream());
        this.contentLength = this.decryptInputStream.available();
        rsaServerUtil.verify(this.decryptInputStream, request.getHeader(SimpleRSAFilter.SIGNATURE_HEADER_NAME));
    }

    @Override
    public int getContentLength() {
        return this.contentLength;
    }

    @Override
    public long getContentLengthLong() {
        return getContentLength();
    }

    @Override
    public BufferedReader getReader() {
        return new BufferedReader(new InputStreamReader(getInputStream()));
    }

    @Override
    public ServletInputStream getInputStream() {
        return new ServletInputStream() {

            @Override
            public int read() {
                try {
                    index = decryptInputStream.read();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                return index;
            }

            @Override
            public boolean isFinished() {
                try {
                    return isReady() && index == decryptInputStream.available();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public boolean isReady() {
                return Objects.nonNull(decryptInputStream);
            }

            @Override
            public void setReadListener(ReadListener listener) {
            }
        };
    }
}